from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from Until.ParseConfigFile import ParserConfigFile
from Until.logger import Log
from Until.until import find_element


class LoginPage:

    def __init__(self,wd):
        self.wd = wd
        self.parse = ParserConfigFile()
        self.loginLocation = self.parse.getSection('freshMarketLogin')
        self.log = Log()

    def click_Viplogin(self):
        '''
        点击会员登录按钮进入登录界面
        :return:
        '''
        try:
            self.log.info('开始点击会员登录按钮')
            locationType,expression = self.loginLocation['login_page.viploginclick'].split(':')
            find_element(self.wd,locationType,expression).click()
        except Exception as e:
            print(e)

    def input_username(self,username):
        '''
        输入用户名
        :param username:
        :return:
        '''
        try:
            self.log.info('开始输入用户名')
            locationType, expression = self.loginLocation['login_page.username'].split(':')
            find_element(self.wd,locationType,expression).send_keys(username)
        except Exception as e:
            print(e)

    def input_password(self,password):
        '''
        输入密码
        :param password:
        :return:
        '''
        try:
            self.log.info('开始输入密码')
            locationType, expression = self.loginLocation['login_page.password'].split(':')
            find_element(self.wd,locationType,expression).send_keys(password)
        except Exception as e:
            print(e)

    def click_SubmitLogin(self):
        '''
        点击登录按钮
        :return:
        '''
        try:
            self.log.info('点击登录按钮')
            locationType, expression = self.loginLocation['login_page.submitclick'].split(':')
            find_element(self.wd,locationType,expression).click()
        except Exception as e:
            print(e)


if __name__ == '__main__':
    chromeDriver_path = 'E:\Python\chromedriver.exe'
    options = Options()
    options.add_argument('--start-maximized')
    wd = webdriver.Chrome(executable_path=chromeDriver_path, options=options)
    wd.get('http://www.cherryzqr.top/fsmarket/index.php')

    loginPage = LoginPage(wd)
    loginPage.click_Viplogin()
    loginPage.input_username('cherry1')
    loginPage.input_password('123456')
    loginPage.click_SubmitLogin()
    sleep(2)
    wd.quit()